package com.manager.ctroller;

import com.example.utils.Utils;
import com.manager.dao.UserDao;
import org.apache.commons.dbutils.QueryRunner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author mucd
 * 2021年06月25日  22:16
 */

@WebServlet(value = "/Delete")
public class Delete extends HttpServlet implements UserDao {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Click");
        String id = req.getParameter("id");
        System.out.println(id);
        if (delete(Integer.parseInt(id)) <= 0) {
            System.out.println("没删除");
        }
        req.getRequestDispatcher("limit?page=1").forward(req,resp);

    }

    /**
     * 删除
     */
    @Override
    public int delete(int id) {
        QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());
        try {
            int delete = queryRunner.update(
                    "delete from manager.person where id = ?"
                    , id
            );
            if (delete > 0){
                return delete;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}